#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#


from cell.mutator import Mutator
from cell.scanner import Scanner

from cell.keeper import Keeper

class   Storer(object) :
    def __init__(self) :
        self._default_mutator = None
        self._default_scanner = None

        self._cell_keeper = None
        ## __init__()

    def __del__(self) :
        self.shutdown()
        ## __del__()

    def startup(self, path_list) :
        self._cell_keeper = Keeper(path_list)
        self._cell_keeper.startup()

        self._default_mutator = Mutator(self._cell_keeper)
        self._default_scanner = Scanner(self._cell_keeper)
        ## startup()

    def shutdown(self) :
        if self._default_mutator :
            self._default_mutator = None

        if self._default_scanner :
            self._default_scanner = None

        if self._cell_keeper :
            self._cell_keeper.shutdown()
            self._cell_keeper = None
        ## shutdown()

    ####-----------------------------------------------------------####

    def insert(self, table, key, value, timestamp_ms) :
        response = self._default_mutator.insert(table, key, value, timestamp_ms)

        return response
        ## insert()

    def insertunique(self, table, key, value, timestamp_ms) :
        response = self._default_mutator.insertunique(table, key, value, timestamp_ms)

        return response
        ## insertunique()

    def erase(self, table, key, timestamp_ms) :
        response = self._default_mutator.erase(table, key, timestamp_ms)

        return response
        ## erase()

    def find(self, table, key, timestamp_ms=0) :
        response = self._default_scanner.find(table, key, timestamp_ms)

        return response
        ## find()

    def findall(self, table, key, timestamp_ms=0) :
        raise NotImplementedError
        ## findall()

    ####-----------------------------------------------------------####

    def createTablet(self, table, key_begin, key_end) :
        response = self._default_mutator.createCell(table, key_begin, key_end)

        return response
        ## createTablet()

    def dropTablet(self, table, key_begin, key_end) :
        response = self._default_mutator.dropCell(table, key_begin, key_end)

        return response
        ## dropTablet()

    ####-----------------------------------------------------------####

    def getAvailableSpace(self) :
        if self._cell_keeper :
            return  self._cell_keeper.sumAvailableSpace()

        return 0
        ## getAvailableSpace()

    ####-----------------------------------------------------------####

    def setCellVolume(self, cell_volume) :
        # this method just work in debugging mode
        if self._cell_keeper :
            self._cell_keeper.setCellVolume( cell_volume )
        ## setCellVolume()

    ## class Storer

